/*
 
 */
#include <avr/interrupt.h>
#include <avr/io.h>

#include <NewSoftSerial.h>

#define INIT_TIMER_COUNT 6
#define RESET_TIMER2 TCNT2 = INIT_TIMER_COUNT

// One and two phases
// 60000ms / 48steps = 1250ms/step
#define TRACK  1250


int ledPin = 13;    // LED connected to digital pin 9
volatile int tmp = 0;
int input = 0;
volatile int delayvalue = 500;
bool led_state = LOW;
int int_counter = 0;

NewSoftSerial Xport(4 /*Rx*/,3 /*Tx*/);

// Aruino runs at 16 Mhz, so we have 1000 Overflows per second...
// 1/ ((16000000 / 64) / 256) = 1 / 1000
ISR(TIMER2_OVF_vect) {
  RESET_TIMER2;
  int_counter++;
  if(!(int_counter%TRACK))
  {
    led_state = not led_state;
    digitalWrite(ledPin, led_state);   // set the LED on/off
  }
};


// The setup() method runs once, when the sketch starts
void setup()   {                
  // initialize the digital pin as an output:
  pinMode(ledPin, OUTPUT);  
  Serial.begin(57600); 
 
  Xport.begin(57600);
  Xport.flush(); 
   
  //Timer2 Settings: Timer Prescaler /64,
  TCCR2A |= (1<<CS22);
  TCCR2A &= ~((1<<CS21) | (1<<CS20));
  // Use normal mode
  TCCR2A &= ~((1<<WGM21) | (1<<WGM20));
  // Use internal clock - external clock not used in Arduino
  ASSR |= (0<<AS2);
  //Timer2 Overflow Interrupt Enable
  TIMSK2 |= (1<<TOIE2) | (0<<OCIE2A);
  RESET_TIMER2;
  sei();
}

// the loop() method runs over and over again,
// as long as the Arduino has power
void loop()                     
{
  while (Serial.available() > 0) {
    input = Serial.read();
    Xport.print((char)input);
  }
  while(Xport.available() > 0){
    Serial.print((char)Xport.read());    
  }
}





